package net.oktaeder.office.model;

import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 

/**
 * Class used to represent company in system
 * 
 * @author tuukka
 *
 */
public class Company {

	private static Log log = LogFactory.getLog(Company.class);
	private Hashtable<String, Employee> employees;
	private Hashtable<String, Project> projects;
	private Hashtable<String, Department> departments;
	
	/**
	 * Default constructor
	 *
	 */
	public Company() {
		log.trace("Creating an instance");
		employees = new Hashtable<String, Employee>();
		projects = new Hashtable<String, Project>();
		departments = new Hashtable<String, Department>();
	}
	
	/**
	 * Adds a new employee to the company roster
	 * 
	 * @param person employee to add to the company
	 */
	public void addEmployee(Employee person) {
		//TODO: check for collision
		employees.put(person.getPIN(), person);
	}
	
	/**
	 * Retrieves an employee from the company roster based on
	 * his PIN.
	 * 
	 * @param pin personal identification code of the employee
	 * @return Employee if found, otherwise null
	 */
	public Employee getEmployee(String pin) {
		return employees.get(pin);
	}
	
	/**
	 * Retreves and employee from the company roster based on
	 * his name.
	 * 
	 * @param name name of the employee
	 * @return Employee if found, otherwise null
	 */
	public Employee getEmployeeByName(String name) {
		Enumeration keys;
		String key;
		Employee employee;
		Employee match = null;
		boolean notFound = true;
		
		keys = employees.keys();
		
		while (keys.hasMoreElements() && notFound) {
			//TODO: see if you can get rid of the type cast
			key = (String)keys.nextElement();
			employee = employees.get(key);
			if (employee.getName().equals(name)) {
				notFound = false;
				match = employee;
			}
		}
				
		return match;
	}

	/**
	 * Adds a new project into company projects
	 * 
	 * @param project Project to add
	 */
	public void addProject(Project project) {
		//TODO: check for collision
		projects.put(project.getName(), project);
	}
	
	/**
	 * Adds a new department into company
	 * 
	 * @param department Department to add
	 */
	public void addDepartment(Department department) {
		departments.put(department.getName(), department);
	}
	
	/**
	 * Retrieves a department based on the name
	 * 
	 * @param departmentName name of the department to retrieve
	 * @return Deparment or null if not found
	 */
	public Department getDepartment(String departmentName) {
		return departments.get(departmentName);
	}
	
	/**
	 * Removes department from the company
	 * 
	 * @param departmentName name of the department to delete
	 */
	public void deleteDepartment(String departmentName) {
		departments.remove(departmentName);
	}
}
